<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 雇员信息 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-users"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 基本信息 -->
		<div class="widget radius-bordered">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 基本信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-users">
				
			</div>
		</div>
		
		<!-- TODO 角色配置 -->
		<div class="widget radius-bordered " id="editor-panel-groups">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 角色配置 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-groups">
				<div id="tree" data-entity="com.app.miwawa.api.system.entity.Groups"></div>
				
				<div class="widget-footer editor-footer-sm">
					<button type="button" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="top" title="取消" onclick="handlerCancel(this);" data-refresh-table="#table-groups">
						<i class="glyphicon glyphicon-remove"></i>
					</button>
					<button type="button" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="top" title="保存" onclick="handlerTree(this);">
						<i class="glyphicon glyphicon-ok"></i>
					</button>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="editor-form-users" action="/miwawa/default/users/update" method="post" 
			name="com.app.miwawa.api.system.entity.center.Users" data-custom-action="true">
			<input type="hidden" name="users.status" value="0">
			<input type="hidden" name="users.invalid" value="F">
</form>
    
<script type="text/javascript">
<!--
	require(['domReady'], function(domReady){
		domReady(function () {
			loadData();
		});
	});
	
	var users;
	function loadData(){
		users = jQuery("#table-users").data("selectedRow");
		
		loadGroups();
		
		jQuery(".editor-body-users").loadform("load", {
			templateId: "#view-users-template",
			templatePath: "/miwawa/content/system/admin/users/template.html",
			templateData: users,
			templateHelper:{
				"userdept":function(value){
					require(['jquery.plugin.form'],function(form){
						form.chosen("#deptsid").select("#deptsid",{ 
							url:"/miwawa/default/common/select.json",
							querys:{"shopsid_EQ":users.shopsid} 
						},function(chosen){
							jQuery("option[value='"+value+"']",chosen).attr("selected","selected").trigger('chosen:updated');
						});
					});
				}				
			},
			onLoadSuccess: function(){
					jQuery("#centerid",'#editor-form-depts').val(centerid);
					
					require(['jquery.plugin.form'],function(form){
						form.tooltip("[data-toggle='tooltip']");
						form.datepicker('.date-selector');
					});
			}
		});
	}
	
	function loadGroups() {
		require(['jquery.bootstrap.tree'],function(tree){
			tree.init("#tree", {
				url: "/miwawa/system/groups/"+users.id+"/tree.json",
				querys: {
					"and": [{"operator":"EQ", "fieldName":"systemid", "fieldValue":"297ebe0e53d648370153d64dbefc0002"}],
					"order": [{"sort":"sort", "order":"asc"}]
				}
			});
		});
	}
	
	function handlerTree(trigger){
		var params = [], nodes = jQuery('#tree').treeview('getChecked');
		
		if(nodes == null || nodes.length <= 0){
			jQuery.message.warning("请选择要配置的角色！");
			return;
		}
		
		if(nodes.length > 1){ 
			jQuery.message.warning("每个人只能配置一个角色！");
			return;
		}
		
		for(var i in nodes){
			var node = nodes[i];
			params.push({
				"source": "Users",
				"sourceid": users.id,
				"target": "Groups",
				"targetid": node.id,
				"status": "0",
				"invalid": "F"
			});
		}
		
		jQuery.ajaxsend({
			url: "/miwawa/system/groups/"+users.id+"/update",
			data: {
				"entityClass": "com.app.miwawa.api.system.entity.center.Relation",
				"beans": params
			},
			success: function(data){
				if(data){
					jQuery(trigger).parent().hide();
				}
			}
		});
	}
//-->
</script>